/* 
 * File:   screen.h
 * Author: bjorn
 *
 * Created on September 2, 2007, 10:55 AM
 */

#ifndef _SCREEN_H
#define	_SCREEN_H

#include <SDL/SDL_ttf.h>


#include "object.h"
#include "image.h"
#include "rope.h"

    
struct Screen {
  Image _surface; 
  
  void 	flip(); 
  int 	high();
  int 	wide();
  void  blit(int tar_x, int tar_y, Image surface); 
  Image make_image(int wide, int high);
  Image set(int wide, int high, int depth, bool fullscreen = true, bool doublebuffer = true);
};  


struct Color : SDL_Color {
	SDL_Color make(const unsigned char r, const unsigned char g, const unsigned char b) {
		SDL_Color result = { r, g, b };
		return result;
	}
};


typedef TTF_Font * Fontpointer; 

struct Preptext {
	Ropelist lines;
	int high;
	int wide;
	int linehigh;
	int linecount;
} ;

struct Font {
	/* Some wrappers around SDL_ttf */
	static Fontpointer load(const char * filename, int size) {
		return TTF_OpenFont(filename, size);
	}
	
	static void free(Fontpointer fontpointer) {
		TTF_CloseFont(fontpointer);
		fontpointer = NULL;
	}
	
	static int linehigh(Fontpointer font) {
		return TTF_FontLineSkip(font);
	}
	
	static int unicharwide(Fontpointer font, const Uint16 ch) {
		int advance;
		TTF_GlyphMetrics(font, ch, NULL, NULL, NULL, NULL, &advance);
		return advance;
	}
	
	static int charwide(Fontpointer font, const char ch) {
		return Font::unicharwide(font, (Uint16) ch);
	}
	
	static Image draw_quick(Fontpointer font, const char * utf8text) {
		SDL_Color fg = {255, 255, 255};
		return TTF_RenderUTF8_Solid(font, utf8text, fg);
	}

	static Image draw(Fontpointer font, const char * utf8text) {
		SDL_Color fg = {255, 255, 255};
		SDL_Color bg = {0, 0, 0};
		return TTF_RenderUTF8_Shaded(font, utf8text, fg, bg);
	}
	
	static Image draw_pretty(Fontpointer font, const char * utf8text) {
		SDL_Color fg = {255, 255, 255};
		return TTF_RenderUTF8_Blended(font, utf8text, fg);
	}

	static int stringwide(Fontpointer font, const char * utf8text) {
		int wide;
		TTF_SizeUTF8(font, utf8text, &wide, NULL);
		return wide;
	}	
	/* Splits text into multiple lines */
	static Ropelist split_multi(Fontpointer font, const Rope text, const int maxwide) ;

	/* Splits text paragraphed with newlines into multiple lines.*/
	static Ropelist split_multi_paragraph
			(Fontpointer font, const char * utf8text, const int maxwide); 

	/*Prepares text for rendering with this font using this maxium width */
	static Preptext prepare_text(Fontpointer font, const char * utf8text, const int maxwide);

	/*Draws prepared text for rendering on this screen with this font using this maxium width. */
	static Image draw_prepared(Screen screen, Fontpointer font, Preptext prepared); 

	/*Prepares text and then draws it on a bitmap. */
	static Image draw_multi
		(Screen screen, Fontpointer font, const char * utf8text, const int maxwide);

};


#endif	/* _SCREEN_H */

